package cn.liyicun.cp.cpweb.service.money;

import cn.hutool.core.util.IdUtil;
import cn.liyicun.cp.cpweb.base.YesNoEnum;
import cn.liyicun.cp.cpweb.entity.MoneyLog;
import cn.liyicun.cp.cpweb.entity.MoneyRecharge;
import cn.liyicun.cp.cpweb.entity.MoneyRechargeExample;
import cn.liyicun.cp.cpweb.entity.UserMoney;
import cn.liyicun.cp.cpweb.mapper.MoneyLogMapper;
import cn.liyicun.cp.cpweb.mapper.MoneyRechargeMapper;
import cn.liyicun.cp.cpweb.mapper.MoneyWithdrawMapper;
import cn.liyicun.cp.cpweb.service.BaseService;
import cn.liyicun.cp.cpweb.service.UserMoneyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class RechargeService extends BaseService implements IMoneyLog {
    @Autowired
    private MoneyRechargeMapper moneyRechargeMapper;
    @Autowired
    private MoneyWithdrawMapper moneyWithdrawMapper;
    @Autowired
    private MoneyLogMapper moneyLogMapper;

    @Autowired
    private UserMoneyService userMoneyService;


    public int recharge(String type, String remark, Long rechargeId, BigDecimal totleMoney, Long uid) {
        MoneyRecharge red = new MoneyRecharge();
        red.setCreateTime(new Date());
        red.setRechargeId(rechargeId);
        red.setRemark(remark);
        red.setStatus(0);
        red.setUid(uid);
        red.setTotleMoney(totleMoney);
        return moneyRechargeMapper.insertSelective(red);

    }


    @Override
    public boolean ext(MoneyTypeEnum type, Long id, YesNoEnum status, String remark,Object param) {
        if (type.compareTo(MoneyTypeEnum.recharge) != 0) return false;

        if (status.compareTo(YesNoEnum.no) == 0) {
            MoneyRecharge rec = new MoneyRecharge();
            rec.setRemark(remark);
            rec.setStatus(2);
            rec.setId(id);
            rec.setUpdateTime(new Date());
            moneyRechargeMapper.updateByPrimaryKeySelective(rec);

        } else if (status.compareTo(YesNoEnum.yes) == 0) {
            Date now=new Date();
            MoneyRecharge recharge = moneyRechargeMapper.selectByPrimaryKey(id);
            Long uid = recharge.getUid();
            BigDecimal changeMoney = recharge.getTotleMoney();

            UserMoney moneyInfo = userMoneyService.getMoneyInfo(uid);
            String oldLocking = moneyInfo.getLocking();
            String newLocking = IdUtil.randomUUID();
            BigDecimal oldMoney = moneyInfo.getMoney();
    BigDecimal newMoney = oldMoney.add(changeMoney);
            int i = userMoneyService.updateMoney(uid, oldLocking, newLocking, newMoney);
            compare(i == 0, "请重试!");
            MoneyLog log = new MoneyLog();
            log.setCreateTime(now);
            log.setUid(uid);
            log.setChangeMoney(changeMoney);
            log.setAfterMoney(newMoney);
            log.setBeforeMoney(oldMoney);
            log.setRemark(remark);
            log.setLocking(newLocking);
            log.setType(type.name());
            log.setTypeId(id.toString());
            moneyLogMapper.insert(log);
            MoneyRecharge rec = new MoneyRecharge();
            rec.setLocking(newLocking);
            rec.setId(id);
            rec.setStatus(1);
            rec.setRemark(remark);
            rec.setUpdateTime(now);
            moneyRechargeMapper.updateByPrimaryKeySelective(rec);
        }
        return true;
    }

    public PageInfo<MoneyRecharge> search(Long uid, int currentPage, int pageSize) {
        MoneyRechargeExample example = new MoneyRechargeExample();
        if (uid != null && uid > 0) {
            example.createCriteria().andUidEqualTo(uid);
        }
        example.setOrderByClause("id desc");
        PageHelper.startPage(currentPage, pageSize);
        List<MoneyRecharge> docs = moneyRechargeMapper.selectByExample(example);
        PageInfo<MoneyRecharge> pageInfo = new PageInfo<>(docs);
        return pageInfo;
    }


}
